package com.ophiux.utils.common;

/**
 * 数字工具类
 * @author chenh
 *
 */
public class DataTypeUtil {

	/**
	 * 判断字符串是否符合Integer类型的规范
	 * @param str
	 * @return
	 */
	public static boolean isIntegerValue(Object value){
		boolean flag = true;
		if(value instanceof Integer){
			return true;
		}
		//否则转换为字符串
		String t_str = value+"";
		try {
			Integer.parseInt(t_str);
		} catch (Exception e) {
			// TODO: handle exception
			flag = false;
		}
		return flag;
	}
	
	/**
	 * 判断字符串是否符合浮点型的规范
	 * @param str
	 * @return
	 */
	public static boolean isFloatValue(Object value){
		boolean flag = true;
		if(value instanceof Float){
			return true;
		}
		//否则转换为字符串
		String t_str = value+"";
		try {
			Float.parseFloat(t_str);
		} catch (Exception e) {
			// TODO: handle exception
			flag = false;
		}
		return flag;
	}
	
	public static void main(String[] args) {
		String value = "";
		System.out.println(isFloatValue(value));
	}
	
	/**
	 * 判断字符串是否符合double类型的规范
	 * @param str
	 * @return
	 */
	public static boolean isDoubleValue(Object value){
		boolean flag = true;
		if(value instanceof Double){
			return true;
		}
		//否则转换为字符串
		String t_str = value+"";
		try {
			Double.parseDouble(t_str);
		} catch (Exception e) {
			// TODO: handle exception
			flag = false;
		}
		return flag;
	}
	
	/**
	 * 判断字符串是否符合boolean类型的值的规范
	 * @param str
	 * @return
	 */
	public static boolean isBooleanValue(Object value){
		boolean flag = false;
		if(value instanceof Boolean){
			return true;
		}
		//否则转换为字符串
		String t_str = value+"";
		try {
			if(t_str != null){
				if(t_str.equals("true")||t_str.equals("false")){
					flag = true;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			flag = false;
		}
		return flag;
	}
	
	/**
	 * 判断字符串是否符合long类型的值的规范
	 * @param str
	 * @return
	 */
	public static boolean isLongValue(Object value){
		boolean flag = true;
		if(value instanceof Long){
			return true;
		}
		//否则转换为字符串
		String t_str = value+"";
		try {
			 Long.parseLong(t_str);
		} catch (Exception e) {
			// TODO: handle exception
			flag = false;
		}
		return flag;
	}
	
	/*********以下是对数据类型的判断**********/
	public static boolean isInteger(Class<?> clzz){
		boolean flag = false;
		try {
			if(Integer.class.isAssignableFrom(clzz)
					||clzz.getName().equals("int")){
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
	}
	
	public static boolean isFloat(Class<?> clzz){
		boolean flag = false;
		try {
			if(Float.class.isAssignableFrom(clzz)
					||clzz.getName().equals("float")){
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
	}
	
	public static boolean isDouble(Class<?> clzz){
		boolean flag = false;
		try {
			if(Float.class.isAssignableFrom(clzz)
					||clzz.getName().equals("double")){
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
	}
	
	public static boolean isLong(Class<?> clzz){
		boolean flag = false;
		try {
			if(Float.class.isAssignableFrom(clzz)
					||clzz.getName().equals("long")){
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
	}
	
	public static boolean isBoolean(Class<?> clzz){
		boolean flag = false;
		try {
			if(Boolean.class.isAssignableFrom(clzz)
					||clzz.getName().equals("boolean")){
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
	}
	
}
